//
//  LCTaskDetailTableViewCell.m
//  lechuang
//
//  Created by Mr_hi on 2023/4/12.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCTaskDetailTableViewCell.h"
#import "ImageBrowserViewController.h"

@interface LCTaskDetailTableViewCell ()<GetImageDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *tipImgView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tipImageViewH;

@property (assign, nonatomic)CGFloat cellHeight;

@end


@implementation LCTaskDetailTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.tipImgView whenTapped:^{
        if (self.tipImgStr) {
            [self showImageWithImg:self.tipImgStr];
        }
    }];
    [LCNotiCenter addObserver:self selector:@selector(handleTextChanged) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

+(instancetype)titleCell {
    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil][0];
}

+(instancetype)imageCell {
    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil][1];
}

-(void)setTipImgStr:(NSString *)tipImgStr {
    _tipImgStr = tipImgStr;
    [self.tipImgView sd_setImageWithURL:[LCFunction XWX_URLImg:tipImgStr] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (!error) {
            CGSize bigSize = image.size;
            CGFloat viewW = (ScreenW-73)/2;
            self.tipImageViewH.constant = ceilf(viewW*bigSize.height/bigSize.width);
            self.cellHeight = ceilf(viewW*bigSize.height/bigSize.width) + 14;
        }
    }];
}

-(void)setShowImgStr:(NSString *)showImgStr {
    _showImgStr = showImgStr;
    [self.showImgView sd_setImageWithURL:[LCFunction XWX_URLImg:showImgStr] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if (!error) {
            if (!self.tipImgStr.length) {
                CGSize bigSize = image.size;
                CGFloat viewW = (ScreenW-73)/2;
                self.tipImageViewH.constant = ceilf(viewW*bigSize.height/bigSize.width);
                self.cellHeight = ceilf(viewW*bigSize.height/bigSize.width) + 14;
            }
            self.showImgView.image = image;
        }
    }];
}

- (void)showImageWithImg:(NSString *)imgStr {
    NSString *urlImg = [LCFunction XWX_URL_StrImg:imgStr];
    // 查看大图
    [ImageBrowserViewController show:self.parentVC type:2 index:0 imagesBlock:^NSMutableArray *{
        return @[urlImg].mutableCopy;
    }];
}

- (IBAction)upLoadBtnAction:(UIButton *)sender {
    [[GetImage shareUploadImage] showActionSheetInFatherViewController:self.parentVC delegate:self whetherNeedEditMode:NO withOnlyPhone:NO];
}

#pragma mark - GetImage-Delegate-选择图片的代理实现
-(void)getImageToActionWithImage:(UIImage *)image {
    NSString * time = [NSString ns_dateConversionsTimeString:[NSDate date] dateFormat:@"yyyyMMdd"];
    [[XWXNetWorkQuery sharedManage] AfPostUrl:Url(URLuploadData) params:@{@"SaveDirercoty":time}.mutableCopy HaveToken:YES imgsData:@{@"Filename":image}.mutableCopy completionHandle:^(id  _Nonnull result) {
        if ([[result objectForKey:@"success"] boolValue]) {
            // 转上传图片的要求
            self.uploadBtn.hidden = YES;
            self.showImgView.hidden = NO;
            self.deleteImgBtn.hidden = NO;
            self.showImgStr = result[@"url"];
            if (!self.tipImgStr.length) {
                CGSize bigSize = image.size;
                CGFloat viewW = (ScreenW-73)/2;
                self.cellHeight = ceilf(viewW*bigSize.height/bigSize.width) + 14;
            }
            if (self.upLoadBlock) {
                self.upLoadBlock(result[@"url"], self.cellHeight);
            }
        } else {
            [self showErrorText:[result objectForKey:@"tips"]];
        }
    } errorHandle:^(NSError * _Nonnull error) {
        XWXNetError;
    }];
}

- (IBAction)deleteBtnAction:(UIButton *)sender {
    self.uploadBtn.hidden = NO;
    self.showImgView.hidden = YES;
    self.showImgView.image = nil;
    self.deleteImgBtn.hidden = YES;
    if (!self.tipImgStr.length) {
        self.tipImageViewH.constant = 26;
    }
    if (self.deleteBlock) {
        self.deleteBlock();
    }
}

+(instancetype)copyDataCell {
    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil][2];
}

- (IBAction)copyBtnAction:(UIButton *)sender {
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    NSString*string = self.dataLb.text;
    [pab setString:string];
    if(pab == nil) {
        [self showErrorText:@"复制失败"];
    }else{
        [self showSuccessText:@"复制成功"];
    }
}

+(instancetype)fieldCell {
    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil][3];
}

- (void)handleTextChanged {
    if (!self.contentTfl.markedTextRange) {
        if (self.changeContentBlock) {
            self.changeContentBlock(self.contentTfl.text);
        }
    }
}

@end
